home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / PeerAware 1.03 / PeerAware-Setup.exe / Html / scripts / JH.Utilities.js < prev    next >
Text File  |  2007-01-14  |  6KB  |  213 lines

  1. ∩╗┐var JH;
  2. if (!JH) JH = {};
  3. else if (typeof JH != "object")
  4.     throw new Error("JH already exists and is not an object");
  5.     
  6. if (JH.Utilities)
  7.     throw new Error("JH.Utilities already exsists");
  8.  
  9. JH.Utilities = {
  10.     HtmlEncode: function(textToEncode)
  11.     {
  12.         var result = textToEncode;
  13.  
  14.         var amp = /&/gi;
  15.         var gt = />/gi;
  16.         var lt = /</gi;
  17.         var quot = /"/gi;
  18.         var apos = /'/gi;
  19.  
  20.         var html_gt = ">";
  21.         var html_lt = "<";
  22.         var html_amp = "&";
  23.         var html_quot = """;
  24.         var html_apos = "'";
  25.  
  26.         result = result.replace(amp, html_amp);
  27.         result = result.replace(quot, html_quot);
  28.         result = result.replace(lt, html_lt);
  29.         result = result.replace(gt, html_gt);
  30.         result = result.replace(apos, html_apos);
  31.  
  32.         return result;
  33.     },
  34.     
  35.     HtmlDecode: function(textToDecode)
  36.     {
  37.         var result = textToDecode;
  38.  
  39.         var amp = /&/gi;
  40.         var gt = />/gi;
  41.         var lt = /</gi;
  42.         var quot = /"/gi;
  43.         var apos = /'/gi;
  44.         var nbsp = / /gi;
  45.  
  46.         var html_gt = ">";
  47.         var html_lt = "<";
  48.         var html_amp = "&";
  49.         var html_quot = "\"";
  50.         var html_apos = "'";
  51.         var html_nbsp = " ";
  52.  
  53.         result = result.replace(amp, html_amp);
  54.         result = result.replace(quot, html_quot);
  55.         result = result.replace(lt, html_lt);
  56.         result = result.replace(gt, html_gt);
  57.         result = result.replace(apos, html_apos);
  58.         result = result.replace(nbsp, html_nbsp);
  59.  
  60.         return result;
  61.     },
  62.     
  63.     HtmlEncode2: function(textToEncode)
  64.     {
  65.         var result = "";
  66.         for (var i = 0; i<textToEncode.length; i++)
  67.         {
  68.             var currentCharCode = textToEncode.charCodeAt(i);
  69.             result += "&#" + currentCharCode + ";";
  70.         }
  71.         return result;
  72.     },
  73.  
  74.     HtmlDecode2: function(textToDecode)
  75.     {
  76.         var result = "";
  77.         
  78.         if (textToDecode.indexOf("&") < 0)
  79.         {
  80.             result = textToDecode;
  81.         }
  82.         else
  83.         {
  84.             for (var i=0; i<textToDecode.length; i++)
  85.             {
  86.                 var charCurrent = textToDecode.charAt(i);
  87.                 var currentEntity = "";
  88.                 if (charCurrent == "&")
  89.                 {
  90.                     var endIndex = textToDecode.indexOf(";", i+1);
  91.                     if (endIndex > 0)
  92.                     {
  93.                         var entity = textToDecode.substring(i+1, endIndex);
  94.                         if ((entity.length > 1) && (entity.charAt(0) == "#"))
  95.                         {
  96.                             try
  97.                             {
  98.                                 if ((entity.charAt(1) == "x") || (entity.charAt(1) == "X"))
  99.                                 {
  100.                                     currentEntity = String.fromCharCode(entity.substring(2));
  101.                                 }
  102.                                 else
  103.                                 {
  104.                                     currentEntity = String.fromCharCode(entity.substring(1));
  105.                                 }
  106.                             }
  107.                             catch(e)
  108.                             {
  109.                                 i++;
  110.                             }
  111.                         }
  112.                     }
  113.                 }
  114.                 result += currentEntity;
  115.             }
  116.         }
  117.         
  118.         return this.HtmlDecode(result);
  119.     },
  120.  
  121.     UrlEncode: function(textToEncode)
  122.     {
  123.         var result = "";
  124.         for (var i = 0; i<textToEncode.length; i++)
  125.         {
  126.             var currentCharCode = textToEncode.charCodeAt(i).toString(16);
  127.             result += "%" + currentCharCode;
  128.         }
  129.         return result;
  130.     },
  131.    
  132.     UrlDecode: function(textToDecode)
  133.     {
  134.         var result = "";
  135.         
  136.         if (textToDecode.indexOf("%") < 0)
  137.         {
  138.             result = textToDecode;
  139.         }
  140.         else
  141.         {
  142.             for (var i=0; i<textToDecode.length; i++)
  143.             {
  144.                 var charCurrent = textToDecode.charAt(i);
  145.                 var currentEntity = "";
  146.                 if (charCurrent == "%")
  147.                 {
  148.                     var endIndex = textToDecode.indexOf("%", i+1);
  149.                     if (endIndex < 0)
  150.                     {
  151.                         endIndex = textToDecode.length;
  152.                     }
  153.                     var entity = textToDecode.substring(i+1, endIndex);
  154.                     currentEntity = String.fromCharCode(parseInt(entity,16));
  155.                     i += entity.length;
  156.                 }
  157.                 result += currentEntity;
  158.             }
  159.         }
  160.         return result;
  161.     },
  162.    
  163.     XmlEncode: function(textToEncode)
  164.     {
  165.         var result = textToEncode;
  166.  
  167.         var amp = /&/gi;
  168.         var gt = />/gi;
  169.         var lt = /</gi;
  170.         var quot = /"/gi;
  171.         var apos = /'/gi;
  172.  
  173.         var xml_gt = ">";
  174.         var xml_lt = "&#60;";
  175.         var xml_amp = "&#38;";
  176.         var xml_quot = """;
  177.         var xml_apos = "'";
  178.  
  179.         result = result.replace(amp, xml_amp);
  180.         result = result.replace(quot, xml_quot);
  181.         result = result.replace(lt, xml_lt);
  182.         result = result.replace(gt, xml_gt);
  183.         result = result.replace(apos, xml_apos);
  184.         
  185.         return result;
  186.     },
  187.     
  188.     XmlDecode: function(textToDecode)
  189.     {
  190.         var result = textToDecode;
  191.  
  192.         var gt = />/gi;
  193.         var lt = /&#60;/gi;
  194.         var amp = /&#38;/gi;   
  195.         var quot = /"/gi;
  196.         var apos = /'/gi;
  197.  
  198.         var xml_gt = ">";
  199.         var xml_lt = "<";
  200.         var xml_amp = "&";
  201.         var xml_quot = "\"";
  202.         var xml_apos = "'";
  203.  
  204.          result = result.replace(amp, xml_amp);
  205.         result = result.replace(quot, xml_quot);
  206.         result = result.replace(lt, xml_lt);
  207.         result = result.replace(gt, xml_gt);
  208.         result = result.replace(apos, xml_apos);
  209.         
  210.         return result;
  211.     }
  212. };
  213.